#coding:utf-8
'''
Created on 2013-6-19

@author: charliu
'''
import sys
import optparse
from basemonitor import BaseMonitor
from pylib import android_commands, surface_stats_collector

class FPSMonitor(BaseMonitor):
    
    def __init__(self, activity, frequency, file_name, device=None):
        BaseMonitor.__init__(self, file_name, ['Android QQ'])
        self._startCollect = False
        self._fps_list = []
        self._adb = android_commands.AndroidCommands(device);
        self._collector = surface_stats_collector.SurfaceStatsCollector(self._adb, activity, frequency)
        self._collector.Start()

    def getInfo(self):
        results = self._collector.SampleResults()
        if not results:
            return None
        else:
            print(str(results[0].value))
            return [results[0].value];
    
    def stop(self):
        BaseMonitor.stop(self)
        print('collect stop ...collect stop ...collect stop ...collect stop ...')
        self._collector.Stop()

def main(argv):
    usage = u"""
      使用方法：
      example: python fps.py -a com.tencent.mobileqq/com.tencent.mobileqq.activity.MainActivity
      -a --activity 指定要测试的 Activity,(必填参数)
      -f --frequency 采样频率，单位为秒，默认为0.5s （可选参数)
      -d --device 有多个手机连接电脑，需指定 Serial number（可选参数)
      -o --outfile 测试结果输出文件名,默认为 fps_result.csv（可选参数)
    """
    parser = optparse.OptionParser(usage)
    parser.add_option('-d', '--device', help='Serial number of device we should use.')
    parser.add_option('-f', '--frequency', dest='frequency', default=0.5, type='float', help='Time in seconds to sleep between updates.')
    parser.add_option('-a', '--activity', dest='activity', help='The activity you want to test, like this: com.tencent.mobileqq/com.tencent.mobileqq.activity.ChatActivity')
    parser.add_option('-o', '--outfile', dest='outfile', default='fps_result.csv', help='The result output file')
    
    options, args = parser.parse_args(argv)
    if not options.activity:
        print(u'-a 参数--activity不能为空')
        parser.print_help()
        exit(0)
    fps_monitor = FPSMonitor(options.activity, options.frequency, options.outfile)
    fps_monitor.start(options.frequency)
if __name__ == '__main__':
    main(sys.argv)
